# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : Variable.py
# Time       ：2023/12/14 23:09
# Author     ：Y-aong
# version    ：python 3.10
# Description：变量操作
"""
import json
from typing import Union

from pydantic import Field

from conf.config import OrderLinesConfig
from orderlines.libraries.BaseTask import BaseTask
from orderlines.utils.base_orderlines_type import BasePluginParam, VariableParam


class Str2IntParam(BasePluginParam):
    string: str = Field(description='字符串')


class Int2StrParam(BasePluginParam):
    number: int = Field(description='数字')


class Json2ListParam(BasePluginParam):
    json_str: str = Field(description='json字符串')


class Json2DictParam(BasePluginParam):
    json_str: str = Field(description='json字符串')


class ToJsonParam(BasePluginParam):
    item: Union[dict, list] = Field(description='字典/列表')


class Variable(BaseTask):
    version = OrderLinesConfig.version
    title = '变量操作'
    category = '内置功能'
    icon = 'Tools'

    def __init__(self):
        super(Variable, self).__init__()

    def str_to_int(self, str2int: Str2IntParam):
        """字符转数字"""
        return {'number': int(str2int.string), 'status': self.success}

    def int_to_str(self, int2str: Int2StrParam):
        """数字转字符"""
        return {'number': str(int2str.number), 'status': self.success}

    def json_to_list(self, json2list: Json2ListParam):
        """json转列表"""
        return {'number': json.loads(json2list.json_str), 'status': self.success}

    def json_to_dict(self, json2dict: Json2DictParam):
        """json转字典"""
        return {'number': json.loads(json2dict.json_str), 'status': self.success}

    def to_json(self, to_json_param: ToJsonParam):
        """转为json"""
        return {'number': json.dumps(to_json_param.item), 'status': self.success}

    def variable_reassign(self, variable_param: VariableParam):
        """
        变量赋值
        Variable assignment, reassigning a new value to a variable
        @param variable_param:
        @return:
        """
        from orderlines.variable.variable_handler import set_variable
        set_variable(**variable_param.model_dump())
        return {'status': self.success}
